const express = require("express");
const axios = require("axios");
const router = express.Router();
const User = require("../db/model/userModel");
const Mail = require("../utils/mail");
const { setToken, getTokenInfo } = require("../utils/token");
const { getCounter } = require("../utils/counter");

// 内存中存入验证码
let codes = {};

/**
 * @api {post} /user/reg 用户注册
 * @apiName 用户注册
 * @apiGroup User
 *
 * @apiParam {String} us 用户名（邮箱）
 * @apiParam {String} ps 用户密码
 * @apiParam {code} ps 验证码
 *
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "code": "200",
 *       "msg": "创建成功"
 *     }
 */
router.get("/getOpenid", (req, res) => {
  const code = req.query.code;
  if (!code) return res.send({ code: 500, msg: "缺少参数" });

// https://api.weixin.qq.com/sns/jscode2session?appid=xxx写上自己的&secret=xxx写上自己的&js_code=xxx写上自己的&grant_type=authorization_code
  axios.get('https://api.weixin.qq.com/sns/jscode2session',
    {
      params: {
        appid: 'xxx写上自己的',
        secret: 'xxx写上自己的',
        'js_code': code,
        grant_type: 'authorization_code',
      }
    })
    .then(response => {

      console.log(response)
       
      res.send({ code: 200, data: response.data });
    })
    .catch(err => {
      console.log('Error: ', err.message);
      res.send({ code: 500, msg: "请求错误" });
    });


});

module.exports = router;
